login_validate.js ➔ login   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 31
rs 8.5806
c 0
b 0
f 0
1
var valLogin = 1;
2
var valPass = 1;
3
var errorText = '{"font-size":"12px","color":"#a94442","display":"inline-block","padding": "5px","font-weight": "700"}';
4
errorText = JSON.parse(errorText);
5
var errorInput = '{"outline":"none","border-color":"#a94442"}';
6
errorInput = JSON.parse(errorInput);
7
var removeError = '{"outline":"none","border-color":"#ccc"}';
8
removeError = JSON.parse(removeError);
9
10
11
function showError(key, value)
12
{
13
    key = "#"+key;
14
    var selector = "input"+key;
15
    $(selector).prev("span").remove();
16
    $(key).css(errorInput);
17
    var txt = $("<span></span>").text(value).css(errorText);
18
    $(key).before(txt);
19
}
20
21
function validateEmail(val)
22
{
23
    var re = /^\S+@\w+\.\w+$/;
24
    return re.test(val);
25
}
26
27
function login()
28
{
29
    var re = /^\S+@/;
30
    var val = $("#login").val();
31
    $("input#login").prev("span").remove();
32
    // console.log(val);
33
    if(val === "")
34
    {
35
        valLogin = 1;
36
        showError("login", " *Please enter your email or username");
37
    }
38
    else if(re.test(val))
39
    {
40
        var ret = validateEmail(val);
41
        if(!ret)
42
        {
43
            valLogin = 1;
44
            showError("login", " *Invalid Email");
45
        }
46
        else
47
        {
48
            $("#login").css(removeError);
49
            valLogin = 0;
50
        }
51
    }
52
    else
53
    {
54
        $("#login").css(removeError);
55
        valLogin = 0;
56
    }
57
}
58
59
function passwordLogin()
60
{
61
    var val = $("#passLogin").val();
62
    $("input#passLogin").prev("span").remove();
63
    if(val === "")
64
    {
65
        valLogin = 1;
66
        showError("passLogin", " *Enter Password");
67
    }
68
    else
69
    {
70
        $("#passLogin").css(removeError);
71
        valPass = 0;
72
    }
73
}
74
75
function initLogin()
76
{
77
    login();
78
    passwordLogin();
79
}
80
81
$("#login").blur(function()
82
{
83
    login();
84
});
85
86
87
$("#passLogin").blur(function()
88
{
89
    passwordLogin();
90
});
91
92
function loginCheck()
93
{
94
    var login = $("#login").val();
95
    var password = $("#passLogin").val();
96
    initLogin();
97
    // console.log(login);
98
    if(valLogin === 0 && valPass === 0)
99
    {
100
        var q = {
101
            "login": login,
102
            "password": password
103
        };
104
105
        q = "q=" + JSON.stringify(q);
106
        // console.log(q);
107
        var xmlhttp = new XMLHttpRequest();
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
108
        xmlhttp.onreadystatechange = function()
109
        {
110
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
111
            {
112
                var result = JSON.parse(xmlhttp.responseText);
113
                // console.log(result);
114
                if(result["location"])
115
                {
116
                    location.href = result["location"];
117
                }
118
                $(result).each(function(index, element) {
119
                    showError(element["key"], element["value"]);
120
                });
121
            }
122
        };
123
        xmlhttp.open("POST", "ajax/validate_login.php", true);
124
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
125
        xmlhttp.send(q);
126
    }
127
    else
128
    {
129
        // alert("Enter correct details");
130
        $("#myModal").modal();
131
    }
132
}
133
134